﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace JULONG.WEB.Library
{
    public class file
    {
        public static string createFileName(string Format = "yyyyMMddHHmmssfff-")
        {
            //Format = "yyyyMMddHHmmssfff-";
            System.Guid guid = new Guid();
            guid = Guid.NewGuid();
            string time = DateTime.Now.ToString(Format);
            string str = guid.ToString("D").Split('-')[0];
            return time + str;
        }

        public static bool delete(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath))
                return false;

            //如果是虚拟路径 "~"开头;
            if (filePath.First() == '~')
                filePath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + filePath.TrimStart('~').Replace('/', '\\');

            //System.Diagnostics.Debug.WriteLine(filePath);

            try
            {
                FileInfo file = new FileInfo(filePath);
                file.Delete();
                return true;
            }
            catch
            {
                return false;
            }

        }

        /// <summary>
        /// 得到文件大小
        /// </summary>
        /// <param name="path">文件路径 相对站点根</param>
        /// <returns>文件大小 单位 字节</returns>
        public static long getFileSize(string filePath)
        {
            FileInfo fi;

            try
            {
                fi = new FileInfo(filePath);
                return fi.Length;
            }
            catch
            {
                return 0;
            }
        }
    }
}
